<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Comment;
use App\Services\MakeProductComment\MakeProductComment;
use App\Http\Controllers\Api\V1\Resources\CommentResource;
use App\Services\MakeProductComment\MakeProductCommentRequest;

class CommentsController extends Controller
{
	public function index()
	{
		$pid = (int)request('pid', 0);
		$index = (int)request('index', 0);
		$total = Comment::sProductCommentsCount($pid);
		$comments = Comment::sGetProductComments($pid, $index);

		return response()->json([
			'total' => $total,
			'comments' => $comments,
		]);
	}

    public function product(MakeProductCommentRequest $request)
    {
    	$comment = app(MakeProductComment::class, [
    		'order' => $request->getOrder(),
		    'product' => $request->getProduct(),
		    'data' => $request->data
	    ])->make();

	    return new CommentResource($comment);
    }
}
